<?php

namespace app\admin\controller;

use app\admin\service\OfficialAccountsService;
use think\facade\Request;
use app\common\Error;
use app\common\Common;

class OfficialAccounts extends Base
{
    private $service;

    public function __construct(OfficialAccountsService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染列表页
    public function index()
    {
        return view();
    }

    // 渲染详情页
    public function info()
    {
        return view();
    }

    // 添加公众号
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\OfficialAccounts::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->edit($params);

        if (!$result) {
            return Common::fail(Error::FAIL_MESSAGE);
        }

        return Common::success();
    }

    // 编辑公众号
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\OfficialAccounts::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->edit($params);

        if (!$result) {
            return Common::fail(Error::FAIL_MESSAGE);
        }

        return Common::success();
    }

    // 公众号列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getList($params);

        return Common::success($data);
    }

    // 公众号详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\OfficialAccounts::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getOfficialAccountsDetail($params['id']);
        return Common::success($data);
    }

    // 删除指定公众号
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\OfficialAccounts::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteOfficialAccount($params['id']);

        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }

    // 下拉列表
    public function select()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\OfficialAccounts::class)->scene('select')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getOfficialAccountsSelectList($params['company_id']);
        return Common::success($data);
    }
}